package library
import(
	"errors"

	"fmt"
	//"strconv"
)

type MusicManager struct{
	musics []Music
}

func NewMusicManager() *MusicManager {
	return &MusicManager{make([]Music, 0)}
}

func (m *MusicManager) Len() int {
	return len(m.musics)
}

func (m *MusicManager) Get( index int) (music *Music, err error) {
	if index < 0 || index >= len(m.musics) {
		return nil, errors.New("Index out of range")
	}
	return &m.musics[index], nil
}

func (m *MusicManager) Find(name string) *Music {
	if len(m.musics) == 0 {
		return nil
	}
	for _, n := range m.musics{
		if n.Name == name {
			return &n
		}
	}
	return nil
}

func (m * MusicManager) Add(music *Music) {
	m.musics = append(m.musics, *music)
}

func (m *MusicManager) Remove(index int) *Music {
	if index < 0 || index >= len(m.musics) {
		return nil
	}
	removeMusic := &m.musics[index]
	fmt.Println(index)
	fmt.Println(removeMusic)
	if index < len(m.musics)-1 && index > 0 {
		fmt.Println("aaaaaaaaaaaa")
		m.musics = append(m.musics[:index], m.musics[index+1:]...)
	}else if index == 0 {
		fmt.Println("bbbbbbbbbbbbbbb")
		m.musics = append(make([]Music, 0), m.musics[index+1:]...)
	}else{
		fmt.Println("cccccccccccccc")
		fmt.Println(m.musics)
		fmt.Println(m.musics[:index])
		m.musics = m.musics[:index]
	}
	return removeMusic
}
func (m *MusicManager) RemoveByName (name string) *Music{
	if len(m.musics) == 0 {
		return nil
	}
	for i, v := range m.musics{
		if v.Name == name {

			//i, error := strconv.Atoi(v.Id)
			//if error != nil {
			//	fmt.Println("failted")
			//}
			//fmt.Println(i)
			//fmt.Println(v)
			//return nil
			return m.Remove(i)
		}
	}
	return nil
}